package com.rongchuang.controller;

import com.rongchuang.dto.LoginDTO;
import com.rongchuang.service.LoginService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;


@RestController
@RequestMapping("app")
public class AppController {

    @Autowired
    private LoginService loginService;

    /**
     * 登录
     * @param mobile 账号
     * @param password 密码
     * @return 登录信息
     */
    @PostMapping("login")
    public LoginDTO login(String mobile, String password) {
        return loginService.login(mobile, password);
    }

    /**
     * 退出登录
     */
    @PostMapping("logout")
    public void logout(HttpServletRequest request) {
        // 获取请求头中的 token
        String token = request.getHeader("Authorization");
        loginService.logout(token);
    }

    /**
     * 修改密码
     * @param id 用户id
     * @param oldPassword 老密码
     * @param newPassword 新密码
     */
    @PostMapping("modify-password")
    public void modifyPassword(Long id,
                               String oldPassword,
                               String newPassword) {
        loginService.modifyPassword(id, oldPassword, newPassword);
    }
}
